<template>
  <view class="container">
    <!-- 状态栏占位 -->
    <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
    
    <!-- 搜索栏 -->
    <view class="search-bar">
      <button class="back-btn" @click="navigateBack">
        <uni-icons type="left" size="20" color="#333"></uni-icons>
      </button>
      <view class="search-input-container">
        <input 
          type="text" 
          v-model="searchQuery" 
          class="search-input"
          placeholder="搜索工具名称或功能..."
          @input="handleSearch"
          autofocus
        />
        <button 
          class="clear-btn" 
          v-if="searchQuery" 
          @click="clearSearch"
        >
          <uni-icons type="close" size="16" color="#999"></uni-icons>
        </button>
      </view>
    </view>
    
    <!-- 搜索结果 -->
    <view class="search-results">
      <view class="section-title" v-if="searchQuery && searchResults.length > 0">
        搜索结果 ({{ searchResults.length }})
      </view>
      
      <view class="results-grid" v-if="searchResults.length > 0">
        <view 
          class="tool-card" 
          v-for="tool in searchResults" 
          :key="tool.id"
          @click="navigateToTool(tool)"
          :style="{ backgroundColor: tool.color + '10' }"
        >
          <view class="tool-icon" :style="{ backgroundColor: tool.color }">
            <uni-icons :type="tool.icon" size="24" color="#fff"></uni-icons>
          </view>
          <text class="tool-name">{{ tool.name }}</text>
        </view>
      </view>
      
      <view class="empty-state" v-if="searchQuery && searchResults.length === 0">
        <uni-icons type="empty" size="60" color="#ccc"></uni-icons>
        <text class="empty-text">没有找到 "{{ searchQuery }}" 相关的工具</text>
      </view>
      
      <!-- 热门搜索 -->
      <view class="hot-search" v-if="!searchQuery">
        <view class="section-title">热门搜索</view>
        <view class="hot-tags">
          <view 
            class="hot-tag" 
            v-for="(tag, index) in hotTags" 
            :key="index"
            @click="searchQuery = tag"
          >
            {{ tag }}
          </view>
        </view>
        
        <view class="section-title" style="margin-top: 24px;">常用工具</view>
        <view class="frequently-used">
          <view 
            class="tool-card" 
            v-for="tool in frequentlyUsedTools" 
            :key="tool.id"
            @click="navigateToTool(tool)"
            :style="{ backgroundColor: tool.color + '10' }"
          >
            <view class="tool-icon" :style="{ backgroundColor: tool.color }">
            <i v-if="tool.iconInfo.icon_type=='fa'" :class="tool.iconInfo.icon" style="font-size: 24px; color: white;"></i>
			<uni-icons v-else="tools.icon_type!=='fa'" type="spinner-cycle" size="30"></uni-icons>
            </view>
            <text class="tool-name">{{ tool.name }}</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
// 状态栏高度
const statusBarHeight = ref(0)

// 搜索查询
const searchQuery = ref('')

// 工具数据 - 与主页面保持一致，实际项目中可抽离为公共数据
const tools = ref([
  {
    id: 1,
    name: '计算器',
    iconInfo: {
      icon: 'fa fa-calculator',
      icon_type: 'fa'
    },
    color: '#42b983',
    category: 'tools',
    path: '/pages/tools/calculator'
  },
  {
    id: 2,
    name: '备忘录',
    iconInfo: {
      icon: 'fa fa-sticky-note',
      icon_type: 'fa'
    },
    color: '#3498db',
    category: 'daily',
    path: '/pages/tools/notes'
  },
  {
    id: 3,
    name: '时钟',
    iconInfo: {
      icon: 'fa fa-qrcode',
      icon_type: ''
    },
    color: '#e74c3c',
    category: 'daily',
    path: '/pages/tools/clock'
  },
  {
    id: 4,
    name: '二维码',
    iconInfo: {
      icon: 'fa fa-qrcode',
      icon_type: 'fa'
    },
    color: '#f39c12',
    category: 'tools',
    path: '/pages/tools/qrcode'
  },
  {
    id: 5,
    name: '手电筒',
    iconInfo: {
      icon: 'fa fa-lightbulb',
      icon_type: ''
    },
    color: '#9b59b6',
    category: 'system',
    path: '/pages/tools/flashlight'
  },
  {
    id: 6,
    name: '天气',
    iconInfo: {
      icon: 'fa fa-sun',
      icon_type: ''
    },
    color: '#1abc9c',
    category: 'daily',
    path: '/pages/tools/weather'
  },
  {
    id: 7,
    name: '翻译',
    iconInfo: {
      icon: 'fa fa-language',
      icon_type: 'fa'
    },
    color: '#34495e',
    category: 'study',
    path: '/pages/tools/translate'
  },
  {
    id: 8,
    name: '单位转换',
    iconInfo: {
      icon: 'fa fa-exchange-alt',
      icon_type: ''
    },
    color: '#8e44ad',
    category: 'tools',
    path: '/pages/tools/converter'
  },
  {
    id: 9,
    name: '电池信息',
    iconInfo: {
      icon: 'fa fa-battery-full',
      icon_type: 'fa'
    },
    color: '#27ae60',
    category: 'system',
    path: '/pages/tools/battery'
  },
  {
    id: 10,
    name: '文件管理',
    iconInfo: {
      icon: 'fa fa-folder',
      icon_type: 'fa'
    },
    color: '#d35400',
    category: 'system',
    path: '/pages/tools/files'
  },
  {
    id: 11,
    name: '待办事项',
    iconInfo: {
      icon: 'fa fa-check-square',
      icon_type: 'fa'
    },
    color: '#2980b9',
    category: 'daily',
    path: '/pages/tools/todos'
  },
  {
    id: 12,
    name: '指南针',
    iconInfo: {
      icon: 'fa fa-compass',
      icon_type: 'fa'
    },
    color: '#c0392b',
    category: 'tools',
    path: '/pages/tools/compass'
  }
])

// 热门搜索标签
const hotTags = [
  '计算器', '二维码', '天气', '翻译', '手电筒'
]

// 常用工具 (取前4个)
const frequentlyUsedTools = computed(() => {
  return tools.value.slice(0, 4)
})

// 搜索结果
const searchResults = ref([])

// 处理搜索
const handleSearch = () => {
  if (!searchQuery.value) {
    searchResults.value = []
    return
  }
  
  const query = searchQuery.value.toLowerCase()
  searchResults.value = tools.value.filter(tool => {
    return tool.name.toLowerCase().includes(query) || 
           tool.category.toLowerCase().includes(query)
  })
}

// 清除搜索
const clearSearch = () => {
  searchQuery.value = ''
  searchResults.value = []
}

// 导航回首页
const navigateBack = () => {
  uni.navigateBack()
}

// 导航到工具页面
const navigateToTool = (tool) => {
  // 构造参数字符串
  const query = `id=${tool.id}&name=${encodeURIComponent(tool.name)}&icon=${encodeURIComponent(tool.icon)}&color=${encodeURIComponent(tool.color)}`;

  uni.navigateTo({
    url: `${tool.path}?${query}`
  });
};

// 监听搜索词变化
watch(searchQuery, (newVal) => {
  handleSearch()
})

// 生命周期钩子
onMounted(() => {
  // 获取状态栏高度
  statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
})
</script>

<style scoped>
.container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

.status-bar {
  width: 100%;
}

.search-bar {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background-color: #fff;
  border-bottom: 1px solid #f0f0f0;
}

.back-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  background: transparent;
}

.search-input-container {
  flex: 1;
  position: relative;
  margin-left: 8px;
}

.search-input {
  width: 100%;
  height: 36px;
  background-color: #f5f7fa;
  border-radius: 18px;
  padding: 0 16px 0 16px;
  font-size: 14px;
}

.clear-btn {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  margin: 0;
  background: transparent;
}

.search-results {
  padding: 16px;
}

.section-title {
  font-size: 16px;
  color: #333;
  margin-bottom: 16px;
  font-weight: 500;
}

.results-grid, .frequently-used {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.tool-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px 8px;
  border-radius: 12px;
  transition: all 0.2s ease;
}

.tool-card:active {
  transform: scale(0.95);
  opacity: 0.8;
}

.tool-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

.tool-name {
  font-size: 13px;
  color: #333;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
}

.empty-text {
  color: #999;
  margin-top: 16px;
  font-size: 15px;
  text-align: center;
  padding: 0 20px;
}

.hot-search {
  margin-top: 16px;
}

.hot-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 24px;
}

.hot-tag {
  background-color: #fff;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  color: #333;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}
</style>
    